﻿using System;
using System.Collections.Generic;
using System.Text;
using Savage.DotNetLibrary.DatabaseLibrary;
using NUnit.Framework;

namespace Savage.DotNetLibrary.TestDotNetLibrary.DatabaseLibraryTests
{
    [TestFixture]
    public class TestSqlServerInstance
    {
        private string connectionString = @"Server=.\SQLEXPRESS;Trusted_Connection=true";

        [Test]
        public void TestSqlServerInstanceMethods()
        {
            SqlServerInstance instance = new SqlServerInstance(connectionString);
            //Test that we have a list of databases
            string[] beforeCreate = instance.GetDatabases();

            //If this has worked we should have a database named master
            Assert.IsTrue(Array.IndexOf(beforeCreate, "master") >= 0);

            //Create a new database
            string databaseName = Guid.NewGuid().ToString();
            instance.CreateDatabase(databaseName);

            //Get a list of the databases on the server
            string[] afterCreate = instance.GetDatabases();

            //The new database should now be listed
            Assert.AreEqual(beforeCreate.Length + 1, afterCreate.Length);
            Assert.IsTrue(Array.IndexOf(afterCreate, databaseName) >= 0);

            //Remove the database
            instance.DropDatabase(databaseName);

            string[] afterDrop = instance.GetDatabases();
            Assert.AreEqual(beforeCreate.Length, afterDrop.Length);
            Assert.IsFalse(Array.IndexOf(afterDrop, databaseName) >= 0);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestCreateDatabaseArgumentNull()
        {
            SqlServerInstance instance = new SqlServerInstance(connectionString);
            instance.CreateDatabase(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestDropDatabaseArgumentNull()
        {
            SqlServerInstance instance = new SqlServerInstance(connectionString);
            instance.DropDatabase(null);
        }
    }
}
